/*
 * Pad.cpp
 *
 *  Created on: 2011/12/25
 *      Author: naoki
 */

#include "Pad.h"
#include "NTEngine/NTEngine.h"
#include "NTEngine/Input/Manager.h"
#include "NTEngine/Input/Keyboard.h"
#include "NTEngine/Input/Touch.h"
#include "NTEngine/Math/Vector4.h"
using namespace NTEngine::Input;
using namespace NTEngine::Math;

Pad* Pad::mInstance = 0;

Pad::Pad(){
}

Pad::~Pad(){
}

void Pad::create(){
	ASSERT( !mInstance );
	mInstance = new Pad();
}

void Pad::destroy(){
	ASSERT( mInstance );
	SAFE_DELETE( mInstance );
}

Pad* Pad::instance(){
	return mInstance;
}

bool Pad::isOn( Button b, int id ) const {
	bool r = false;
	//ジョイスティックある？
	Manager m = Manager::instance();
/*	if ( m.joystickNumber() > id ){
		Joystick j = m.joystick( id );
		switch ( b ){
			case JUMP:
				r = ( j.isOn( 0 ) ) ? 1 : 0; //0番ボタンがジャンプ
				break;
			case FIRE:
				r = ( j.isOn( 1 ) ) ? 1 : 0; //1番ボタンが武器
				break;
			case TURN:
				r = ( j.isOn( 2 ) ) ? 1 : 0; //2番ボタンが旋回
				break;
			case UP:
				r = ( j.isOn( Joystick::BUTTON_UP ) ) ? 1 : 0;
				break;
			case DOWN:
				r = ( j.isOn( Joystick::BUTTON_DOWN ) ) ? 1 : 0;
				break;
			case LEFT:
				r = ( j.isOn( Joystick::BUTTON_LEFT ) ) ? 1 : 0;
				break;
			case RIGHT:
				r = ( j.isOn( Joystick::BUTTON_RIGHT ) ) ? 1 : 0;
				break;
			default: ASSERT( false ); break;
		}
	}
*/
	Touch t = m.touch();
	if ( id == 0 ) {
		Vector4 rect;
		switch ( b ) {
		case UP: rect.set( 120, 0, 220, 450 ); break;
		case DOWN: rect.set( 120, 450, 220, 700 ); break;
		case LEFT: rect.set( 0, 190, 320, 290 ); break;
		case RIGHT: rect.set( 320, 190, 640, 290 ); break;
		case JUMP: rect.set( 640, 0, 1200, 100 ); break;
		case FIRE: rect.set( 640, 200, 1200, 400 ); break;
		case TURN: rect.set( 640, 400, 1200, 600 ); break;
		default: ASSERT( false ); break;
		}
		bool area = rect.x < t.x() && t.x() < rect.z;
		area = area && rect.y < t.y() && t.y() < rect.w;
		r = r || ( area && t.isOn() );
	}
	//キーボードを追加
	Keyboard k = m.keyboard();
	if ( id == 0 ){ //1P
		char c = 0;
		switch ( b ){
			case UP: c = 'w'; break;
			case DOWN: c = 'z'; break;
			case LEFT: c = 'a'; break;
			case RIGHT: c = 's'; break;
			case JUMP: c = 'd'; break;
			case FIRE: c = 'x'; break;
			case TURN: c = 'e'; break;
			default: ASSERT( false ); break;
		}
		r = r || k.isOn( c );
	}else if ( id == 1 ){ //2P
		char c = 0;
		switch ( b ){
			case UP: c = 'i'; break;
			case DOWN: c = 'm'; break;
			case LEFT: c = 'j'; break;
			case RIGHT: c = 'k'; break;
			case JUMP: c = 'l'; break;
			case FIRE: c = ','; break;
			case TURN: c = 'o'; break;
			default: ASSERT( false ); break;
		}
		r = r || k.isOn( c );
	}
	return r;
}

bool Pad::isTriggered( Button b, int id ) const {
	bool r = false;
	//ジョイスティックある？
	Manager m = Manager::instance();
	/*
	if ( m.joystickNumber() > id ){
		Joystick j = m.joystick( id );
		switch ( b ){
			case JUMP:
				r = ( j.isTriggered( 0 ) ) ? 1 : 0; //0番ボタンがジャンプ
				break;
			case FIRE:
				r = ( j.isTriggered( 1 ) ) ? 1 : 0; //1番ボタンが武器
				break;
			case TURN:
				r = ( j.isTriggered( 2 ) ) ? 1 : 0; //2番ボタンが旋回
				break;
			case UP:
				r = ( j.isTriggered( Joystick::BUTTON_UP ) ) ? 1 : 0;
				break;
			case DOWN:
				r = ( j.isTriggered( Joystick::BUTTON_DOWN ) ) ? 1 : 0;
				break;
			case LEFT:
				r = ( j.isTriggered( Joystick::BUTTON_LEFT ) ) ? 1 : 0;
				break;
			case RIGHT:
				r = ( j.isTriggered( Joystick::BUTTON_RIGHT ) ) ? 1 : 0;
				break;
			default: ASSERT( false ); break;
		}
	}
	*/
	Touch t = m.touch();
	if ( id == 0 ) {
		Vector4 rect;
		switch ( b ) {
		case UP: rect.set( 120, 0, 220, 450 ); break;
		case DOWN: rect.set( 120, 450, 220, 700 ); break;
		case LEFT: rect.set( 0, 190, 320, 290 ); break;
		case RIGHT: rect.set( 320, 190, 640, 290 ); break;
		case JUMP: rect.set( 640, 0, 1200, 100 ); break;
		case FIRE: rect.set( 640, 200, 1200, 400 ); break;
		case TURN: rect.set( 640, 400, 1200, 600 ); break;
		default: ASSERT( false ); break;
		}
		bool area = rect.x < t.x() && t.x() < rect.z;
		area = area && rect.y < t.y() && t.y() < rect.w;
		r = r || ( area && t.isTriggered() );
	}
	//キーボードを追加
	Keyboard k = m.keyboard();
	if ( id == 0 ){ //1P
		char c = 0;
		switch ( b ){
			case UP: c = 'w'; break;
			case DOWN: c = 'z'; break;
			case LEFT: c = 'a'; break;
			case RIGHT: c = 's'; break;
			case JUMP: c = 'd'; break;
			case FIRE: c = 'x'; break;
			case TURN: c = 'e'; break;
			default: ASSERT( false ); break;
		}
		r = r || k.isTriggered( c );
	}else if ( id == 1 ){ //2P
		char c = 0;
		switch ( b ){
			case UP: c = 'i'; break;
			case DOWN: c = 'm'; break;
			case LEFT: c = 'j'; break;
			case RIGHT: c = 'k'; break;
			case JUMP: c = 'l'; break;
			case FIRE: c = ','; break;
			case TURN: c = 'o'; break;
			default: ASSERT( false ); break;
		}
		r = r || k.isTriggered( c );
	}
	return r;
}
